package com.phiture.erp.sale.web.controller.api;

import cn.hutool.core.collection.CollUtil;
import com.phiture.erp.sale.api.pojo.req.ErpSaleReturnReq;
import com.phiture.erp.sale.api.pojo.resp.ErpSaleReturnItemsResp;
import com.phiture.erp.sale.api.pojo.resp.ErpSaleReturnResp;
import com.phiture.erp.sale.api.service.ErpSaleReturnApi;
import com.phiture.erp.sale.core.service.ErpSaleReturnService;
import com.phiture.erp.sale.dal.entity.ErpSaleReturnDO;
import com.phiture.erp.sale.dal.entity.ErpSaleReturnItemDO;
import com.phiture.erp.sale.web.controller.convert.ErpSaleReturnConvertMapper;
import javafx.util.Pair;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author YH
 * @date 2025-06-16-16:48
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ErpSaleReturnApiServiceImpl implements ErpSaleReturnApi {
    private final ErpSaleReturnService saleReturnService;

    @Override
    public List<ErpSaleReturnResp> getErpSaleReturnList(ErpSaleReturnReq req) {
        List<ErpSaleReturnDO> list = saleReturnService.getSaleReturnList(req.getStatus(), req.getStartDate(), req.getEndDate());
        return ErpSaleReturnConvertMapper.INSTANCE.toErpSaleReturnRespList(list);
    }

    @Override
    public List<ErpSaleReturnItemsResp> getErpSaleReturnItemList(Map<Long, javafx.util.Pair<String, Long>> saleReturnMap) {
        List<ErpSaleReturnItemDO> items = saleReturnService.getSaleReturnItemListByReturnIds(saleReturnMap.keySet());
        if(CollUtil.isEmpty(items)){
            return Collections.emptyList();
        }

        return items.stream().map(item ->{
            ErpSaleReturnItemsResp resp = new ErpSaleReturnItemsResp();
            resp.setSaleReturnId(item.getReturnId());
            Pair<String, Long> pair = saleReturnMap.get(item.getReturnId());
            if(pair != null){
                resp.setSaleReturnNo(pair.getKey());
                resp.setCustomerId(pair.getValue());
            }
            resp.setSaleReturnItemId(item.getId());
            resp.setWarehouseId(item.getWarehouseId());
            resp.setMaterialId(item.getMaterialId());
            resp.setMaterialUnitId(item.getMaterialUnitId());
            resp.setMaterialPrice(item.getMaterialPrice());
            resp.setCount(item.getCount());
            resp.setTotalPrice(item.getTotalPrice());
            resp.setTaxPercent(item.getTaxPercent());
            resp.setTaxPrice(item.getTaxPrice());
            return resp;
        }).collect(Collectors.toList());

    }

    @Override
    public void backfillSaleReturnAccountCheckFlag(Collection<Long> saleReturnIds, Boolean accountCheckFlag) {
        saleReturnService.backfillSaleReturnAccountCheckFlag(saleReturnIds, accountCheckFlag);
    }

    @Override
    public void updateSaleReturnAccountCheckFlag(Collection<String> saleReturnNos, Boolean accountCheckFlag) {
        saleReturnService.updateSaleReturnAccountCheckFlag(saleReturnNos, accountCheckFlag);
    }
}
